# -*- coding: utf-8 -*-
"""
Created on Mon Jul 24 10:11:02 2023

@author: skunk69
"""

# -*- coding: utf-8 -*-
"""
Created on Mon Jul 24 06:32:51 2023

@author: skunk69
"""

from pathlib import Path,PurePath
import json
from text_align import count_char,chinese_align,space_align

file_name = 'LES.json'
input_path = PurePath(Path.cwd().parent,'json_docs',file_name)
with open(input_path, encoding='utf-8') as json_file:
    this_scale = json.load(json_file)

chinese_name = this_scale['chinese_name']
english_name = this_scale['english_name']
abbreviation = this_scale['abbreviation']
outline = this_scale['outline']
contents = this_scale['contents']

instruction = contents['instruction']
items = contents['items']
title_items = contents['title_items']
title_time_rating = contents['title_time_rating']
time_rating = contents['time_rating']
title_characteristics_rating = contents['title_characteristics_rating']
characteristics_rating = contents['characteristics_rating']
title_level_influence_rating = contents['title_level_influence_rating']
level_influence_rating = contents['level_influence_rating']
title_duration_rating = contents['title_duration_rating']
duration_rating = contents['duration_rating']
duration_score_rules = contents['duration_score_rules']
level_influence_score_rules = contents['level_influence_score_rules']
scales = contents['scales']
scales_items = contents['scales_items']

implementation = this_scale['implementation']
measurements = this_scale['measurements']
interpretation = this_scale['interpretation']
applications = this_scale['applications']

# output guide.rst    
with open(this_scale['abbreviation']+'_guide.rst','w+') as f_guide:
    
    line = f'{"="*len(chinese_name)*2}\n'
    f_guide.write(line)
    f_guide.write(f'{chinese_name}\n')
    f_guide.write(line)
    f_guide.write('\n')
    
    english = english_name+', '+abbreviation
    f_guide.write(f'{english}\n')
    f_guide.write('\n')
    
    subtitle = u'概述'
    line = f'{"-"*len(subtitle)*2}\n'
    f_guide.write(line)
    f_guide.write(f'{subtitle}\n')
    f_guide.write(line)
    f_guide.write('\n')
    
    lines = outline.split('\n')
    for line in lines:
        f_guide.write(f'{line}\n')    
        f_guide.write('\n')

    subtitle = u'内容'
    line = f'{"-"*len(subtitle)*2}\n'
    f_guide.write(line)
    f_guide.write(f'{subtitle}\n')
    f_guide.write(line)
    f_guide.write('\n')    
    
    f_guide.write(f'{abbreviation}量表包括了{len(items)}条条目，')
    f_guide.write(f'{len(scales)}个维度，这些维度分别是：')
    for num,scale in enumerate(scales):
        if num < len(scales)-1:
            f_guide.write(f'{scale}，')
        else:
            f_guide.write(f'{scale}。')
    
    f_guide.write('其中，')
    for num,scale in enumerate(scales):
        if num < len(scales)-1:
            f_guide.write(f'{scale}维度由条目{scales_items[num]}组成，')
        else:
            f_guide.write(f'{scale}维度由条目{scales_items[num]}组成。')    

    f_guide.write(f'评价准则由{title_time_rating}、{title_characteristics_rating}、\
                     {title_level_influence_rating}和{title_duration_rating}组成。')

    f_guide.write(f'其中，{title_time_rating}评价准则为：')
    for num,rate in enumerate(time_rating):
        if num < len(time_rating)-1:
            f_guide.write(f'{rate}，')
        else:
            f_guide.write(f'{rate}。')    

    f_guide.write(f'{title_characteristics_rating}评价准则为：')
    for num,rate in enumerate(characteristics_rating):
        if num < len(characteristics_rating)-1:
            f_guide.write(f'{rate}，')
        else:
            f_guide.write(f'{rate}。')    

    f_guide.write(f'{title_level_influence_rating}评价准则为：')
    for num,rate in enumerate(level_influence_rating):
        if num < len(level_influence_rating)-1:
            f_guide.write(f'{rate}，')
        else:
            f_guide.write(f'{rate}。')    

    f_guide.write(f'{title_duration_rating}评价准则为：')
    for num,rate in enumerate(duration_rating):
        if num < len(duration_rating)-1:
            f_guide.write(f'{rate}，')
        else:
            f_guide.write(f'{rate}。')  

    f_guide.write(f'相应地，{title_duration_rating}计分规则为：\
                     {duration_score_rules}，')
    f_guide.write(f'{title_level_influence_rating}计分规则为：\
                     {level_influence_score_rules}。\n')
    f_guide.write('\n') 
    
    subtitle = u'实施方法'
    line = f'{"-"*len(subtitle)*2}\n'
    f_guide.write(line)
    f_guide.write(f'{subtitle}\n')
    f_guide.write(line)
    f_guide.write('\n')
    
    lines = implementation.split('\n')
    for line in lines:
        f_guide.write(f'{line}\n')    
        f_guide.write('\n')
     
    subtitle = u'测量学指标'
    line = f'{"-"*len(subtitle)*2}\n'
    f_guide.write(line)
    f_guide.write(f'{subtitle}\n')
    f_guide.write(line)
    f_guide.write('\n')
    
    reliability = measurements['reliability']
    validity = measurements['validity']
    
    f_guide.write('量表信度::\n')
    f_guide.write('\n')
    f_guide.write(f'  该量表的信度数据：{reliability}\n')
    f_guide.write('\n')
    f_guide.write('量表效度::\n')
    f_guide.write('\n')
    f_guide.write(f'  该量表的效度数据：{validity}\n')
    f_guide.write('\n')
       
    subtitle = u'结果分析方法'
    line = f'{"-"*len(subtitle)*2}\n'
    f_guide.write(line)
    f_guide.write(f'{subtitle}\n')
    f_guide.write(line)
    f_guide.write('\n')    
    
    lines = interpretation.split('\n')
    for line in lines:
        f_guide.write(f'{line}\n')    
        f_guide.write('\n') 
    
    subtitle = u'应用情况'
    line = f'{"-"*len(subtitle)*2}\n'
    f_guide.write(line)
    f_guide.write(f'{subtitle}\n')
    f_guide.write(line)
    f_guide.write('\n')    
    
    lines = applications.split('\n')
    for line in lines:
        f_guide.write(f'{line}\n')    
        f_guide.write('\n')
    f_guide.close() 

# output scale.rst    
with open(this_scale['abbreviation']+'_scale.rst','w+') as f_scale:
    line = f'{"="*len(chinese_name)*2}\n'
    f_scale.write(line)
    f_scale.write(f'{chinese_name}\n')
    f_scale.write(line)
    f_scale.write('\n')
    
    english = english_name+', '+abbreviation
    f_scale.write(f'{english}\n')
    f_scale.write('\n')
    
    subtitle = u'指导语'
    line = f'{"-"*len(subtitle)*2}\n'
    f_scale.write(line)
    f_scale.write(f'{subtitle}\n')
    f_scale.write(line)
    f_scale.write('\n')
    
    lines = instruction.split('\n')
    for line in lines:
        f_scale.write(f'{line}\n')
    
    f_scale.write('\n')

    # construction of RST table with simple grid  
    L1 = 4
    L2 = 20 # items column
    L3 = L1*len(time_rating) # time_rating column
    L4 = L1*len(characteristics_rating) # characteristics_rating column
    L5 = L1*len(level_influence_rating) # level_influence_rating column
    L6 = L1*len(duration_rating) # duration_rating column
    L7 = 6 # remarks column

    # first format line of RST table 
    format_line = '='*L2
    for rate in time_rating:
        format_line = format_line+' '+'='*L1

    for rate in characteristics_rating:
        format_line = format_line+' '+'='*L1
        
    for rate in level_influence_rating:
        format_line = format_line+' '+'='*L1    
        
    for rate in duration_rating:
        format_line = format_line+' '+'='*L1
    
    format_line = format_line+' '+'='*L7
    
    f_scale.write(f'{format_line}\n')
    
    # header line
    LL = L3+len(time_rating)-1-count_char(title_time_rating)
    header_line = f'{u".. ":<{L2}}'+' '+f'{title_time_rating:^{LL}}'
    LL = L4+len(characteristics_rating)-1-count_char(title_characteristics_rating)
    header_line = header_line+' '+f'{title_characteristics_rating:^{LL}}'
    LL = L5+len(level_influence_rating)-1-count_char(title_level_influence_rating)
    header_line = header_line+' '+f'{title_level_influence_rating:^{LL}}'
    LL = L6+len(duration_rating)-1-count_char(title_duration_rating)
    header_line = header_line+' '+f'{title_duration_rating:^{LL}}'
    header_line = header_line+' '+f'{u"备注":>{L7}}'
       
    f_scale.write(f'{header_line}\n')
    
    # merge line    
    merge_line = '-'*L2+' '+'-'*(L3+len(time_rating)-1)
    merge_line = merge_line+' '+'-'*(L4+len(characteristics_rating)-1)
    merge_line = merge_line+' '+'-'*(L5+len(level_influence_rating)-1)
    merge_line = merge_line+' '+'-'*(L6+len(duration_rating)-1)
    merge_line = merge_line+' '+'-'*L7
    f_scale.write(f'{merge_line}\n')
    
    LL = count_char(title_items)
    header_line = f'{title_items:^{L2-LL}}'
    
    cols = time_rating+characteristics_rating+level_influence_rating+duration_rating
    max_cols = max([len(col) for col in cols])    
    cols = space_align(cols)

    for i in range(max_cols):
        for col in cols:
            LL = count_char(col[i])
            header_line = header_line+' '+f'{col[i]:^{L1-LL}}'
        f_scale.write(f'{header_line}\n')
        header_line = f'{u".. ":^{L2}}'
        
    f_scale.write(f'{format_line}\n') 
    
    # table contents 
    for key,value in items.items():
        line_str = key+'.'+value
        LL = count_char(line_str)        
        if LL+len(line_str) > L2:
            LL1 = count_char(line_str[0:int(L2/2)])
            line = f'{line_str[0:int(L2/2)]:<{L2-LL1}}'
            f_scale.write(f'{line}\n')
            LL1 = count_char(line_str[int(L2/2)::])
            line = f'{line_str[int(L2/2)::]:<{L2-LL1}}'
            f_scale.write(f'{line}\n')
        else:
            line = f'{line_str:<{L2-LL}}'
            f_scale.write(f'{line}\n')
    
    f_scale.write(f'{format_line}\n')    
    f_scale.close()